// pages/my/userInfomation/userInfomation.js
const utils = require("../../../utils/utils");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        firstOpen:0,
        showfix:false
    },
    hideAdvs(){
        this.setData({
            showfix:false
        })
    },
    goNick(e){
        var name = e.currentTarget.dataset.name;
        var face = e.currentTarget.dataset.face;
        wx.navigateTo({
          url: '/pages/my/changeNickname/changeNickname?name='+name+'&face='+face,
        })
    },
    bindDateChange: function(e) {
        this.setData({
            birthday: e.detail.value
        })
        this.submitInfo();
    },
    unbindMobile() {
        var that = this;
        if (!utils.is_define(that.data.mobile)) {
            wx.showModal({
                title: '温馨提示',
                content: '您还未绑定手机号，是否去绑定',
                showCancel: true,
                success(res) {
                    if (res.confirm) {
                        wx.redirectTo({
                            url: '/pages/my/bindMobile/bindMobile',
                        })
                    } else if (res.cancel) {

                    }
                }

            })
        }else{
            wx.showModal({
                title: '温馨提示',
                content: '您确定要解绑当前手机号吗？',
                showCancel: true,
                success(res) {
                    if (res.confirm) {
                        wx.redirectTo({
                            url: '/pages/my/unbindMobile/unbindMobile?mobile='+that.data.mobile,
                        })
                    } else if (res.cancel) {

                    }
                }

            })
        }
    },
        getIndex(){
        var that = this;
        app.get_local('AccToken').then(res => {
          if(utils.is_define(res)){
            utils.request('user/userInfo',{}, function (res) {
              if(res.data.code==200){
                that.setData({
                  mobile:res.data.data.tel,
                  nickName:res.data.data.nickName,
                  face:res.data.data.face,
                  birthday:res.data.data.birthday
                })
              }else{
                utils.toast(res.data.msg)
              }
            })
          }
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    
    onLoad(options) {
        this.getIndex();
    },
    //上传头像
    uploadHeadImg() {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                // tempFilePath可以作为 img 标签的 src 属性显示图片
                const tempFilePaths = res.tempFilePaths
                wx.showLoading()
                utils.uploadImg(res.tempFilePaths[0], function (res) {
                    var data = JSON.parse(res.data).data
                    
                    that.setData({
                        face: data.fileUrl
                    })
                    that.submitInfo();
                    
                })
            }
        })
    },
    nickNameInput(e) {
        this.setData({
            nickName: e.detail.value
        })
        this.submitInfo();
    },
    submitInfo() {
        var that = this;
        if (that.data.nickName.length == 0) {
            utils.toast('昵称不能为空');
            return false;
        }
        utils.request('user/edit', {
            nickName: that.data.nickName,
            face: that.data.face,
            birthday:that.data.birthday
        }, function (res) {
                        if(res.data.code==200){
                if(utils.is_define(res.data.points)&&res.data.points>0){
                    wx.hideLoading()
                    that.setData({
                        point:res.data.points,
                        showfix:true
                    })
                }else{
                    wx.hideLoading()
                    utils.toast(res.data.msg)
                }
            }else{
                wx.hideLoading()
                    utils.toast(res.data.msg)
            }
        })
    },
    goshop(){
        wx.navigateTo({
          url: '/subPackages/pages/pointsMall/pointsMall',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this;
        if (that.data.firstOpen == 1) {
          that.getIndex()
        } else {
          that.setData({
            firstOpen: 1
          })
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})